﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.ComponentModel;
using Ferpect.ComponentModel.Design;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Ferpect.ComponentModel.Sprites
{
    [Description("Draws a filled rectangle.")]
#if WINDOWS
    [Designer(Designers.RectangleSpriteDesigner, typeof(System.ComponentModel.Design.IDesigner))]
#endif
    public class RectangleSprite : Sprite
    {
        private Rectangle? fullScreen;
        private Vector2 size;
        private Texture2D texture;

        public RectangleSprite()
        {
            this.size.X = 160f;
            this.size.Y = 90f;
        }

        [Category("Layout")]
        [DefaultValue(false)]
        [RefreshProperties(RefreshProperties.All)]
        public bool FullScreen
        {
            get
            {
                return this.fullScreen.HasValue;
            }
            set
            {
                if (!value)
                {
                    if (this.fullScreen.HasValue)
                    {
                        this.fullScreen = null;
                    }
                }
                else
                {
                    if (!this.fullScreen.HasValue)
                    {
                        this.Position = new Vector2();
                        this.RelativeOrigin = RelativeLocation.TopLeft;
                        this.Origin = new Vector2();
                        this.Scale = 1f;
                    }
                    this.SetFullScreen();
                }
            }
        }

        [Category("Layout")]
        [DefaultValue(160)]
        public int Width
        {
            get { return (int)this.size.X; }
            set
            {
                this.size.X = value;
                this.OnAfterSizeChanged();
            }
        }

        [Category("Layout")]
        [DefaultValue(90)]
        public int Height
        {
            get { return (int)this.size.Y; }
            set
            {
                this.size.Y = value;
                this.OnAfterSizeChanged();
            }
        }

        public override void Draw(SpriteBatchStack spriteBatchStack)
        {
            if (this.texture != null && this.Visible)
            {
                Vector2 scale = Vector2.One * this.Scale;
                scale.X = scale.X * this.size.X / this.texture.Width;
                scale.Y = scale.Y * this.size.Y / this.texture.Height;

                Vector2 origin = this.Origin;
                origin.X = origin.X / (this.size.X / this.texture.Width);
                origin.Y = origin.Y / (this.size.Y / this.texture.Height);

                if (this.fullScreen.HasValue)
                {
                    spriteBatchStack.Current.Draw(this.texture, this.fullScreen.Value, null, this.Color, 0, Vector2.Zero, this.Effects, this.LayerDepth);
                }
                else
                {
                    spriteBatchStack.Current.Draw(this.texture, this.Position, null, this.Color, this.Rotation, origin, scale, this.Effects, this.LayerDepth);
                }
            }
        }

        public override Vector2 GetSize()
        {
            if (this.fullScreen.HasValue)
            {
                Rectangle sizeRect = this.fullScreen.Value;
                return new Vector2(sizeRect.Width, sizeRect.Height);
            }
            return this.size;
        }

        public override void LoadContent(ContentManager contentManager)
        {
            AmbientProperties ambientProperties = (AmbientProperties)this.GetService(typeof(AmbientProperties));
            this.texture = ambientProperties.BlankTexture;

            if (this.fullScreen.HasValue)
            {
                this.SetFullScreen();
            }
            this.ResetRelativeOrigin();
        }

        private void SetFullScreen()
        {
            IViewportService viewport = (IViewportService)this.GetService(typeof(IViewportService));
            if (viewport != null)
            {
                if (this.UseTitleSafeArea)
                {
                    this.fullScreen = viewport.TitleSafeArea;
                }
                else
                {
                    this.fullScreen = viewport.ViewportArea;
                }
                return;
            }
            this.fullScreen = new Rectangle();
        }

        public override void UnloadContent()
        {
            this.texture = null;
        }
    }
}
